---@class TBS.UltimateEffect:TBS.EffectBase
local UltimateEffect = TBS.CreateClass("UltimateEffect","EffectBase")
function UltimateEffect:OnCreate()
    TBS.EffectBase.OnCreate(self)
    self.drawingName = ""
    ---@type GameObject
    self.drawObj = nil
    self.animList = {}
    self.effectTime = 0
    self.effectState = 0
    self.effectPos = Vector3.New(0,0,0)
    self.effectMoveStartPos = Vector3.New(0,-Screen.height/2 - 500,0)   --移动初始位置
    self.effectMoveEndPos = Vector3.New(0,0,0)  --移动终点位置
    self.effectMoveTime = 0.8   --移动时间
    self.effectStayTime = 0.1     --中间停留时间
    self.effectCurve = 3.5    --移动速度曲线
end
function UltimateEffect:OnDestroy()
    TBS.EffectBase.OnDestroy(self)
    self.animList = nil
    TBS.PoolMgr.CancelLoad(self.loadDrawingRequestId)
    TBS.PoolMgr.ReturnPrefabToPool(self.drawingName,self.drawObj)
    self.loadDrawingRequestId = nil
    self.drawObj = nil
    TBS.CameraMgr.EnableUltimateCamera(false)
end
function UltimateEffect:SetDrawingName(name)
    self.drawingName = name
end
function UltimateEffect:Play()
    local isLoading = self.isLoading
    TBS.EffectBase.Play(self)
    if not isLoading then
        self.loadDrawingRequestId = TBS.PoolMgr.LoadPrefabFromPool(self.drawingName,self.LoadDrawingCallback,self)
    end
end

function UltimateEffect:OnLoadDone()
    self.obj:SetActive(false)
    local f = self.obj.transform:Find("f")
    Util.SetLayerMask(f.gameObject,"TransparentFX",true);
    local b = self.obj.transform:Find("b")
    Util.SetLayerMask(b.gameObject,"Default",true);
    local arr = self.obj:GetComponentsInChildren(TSkeletonAnimation)
    for i = 0,arr.Length-1 do
        table.insert(self.animList,arr[i])
    end
    ---@type UnityEngine.Renderer[]
    local renders = self.obj:GetComponentsInChildren(TRenderer)
    if renders then
        for k = 0,renders.Length-1 do
            renders[k].sortingOrder = 2
        end
    end
    self:InitDrawing()
end
function UltimateEffect:LoadDrawingCallback(obj)
    self.drawObj = obj
    self.loadDrawingRequestId = nil
    self:InitDrawing()
end
function UltimateEffect:InitDrawing()
    if not self.obj or not self.drawObj then
        return
    end
    self:DoneCallback()
    self.obj:SetActive(true)
    self.drawObj:SetActive(true)
    local tran = self.drawObj.transform
    Util.SetLayerMask(self.drawObj,"UI",true);
    tran:SetParent(TBS.CameraMgr.canvas.transform,false)
    Util.Lua_SetLocalPositionXYZ(self.drawObj,0,0,0)
    local modelId = TBS.ActorMgr.GetBoss():GetModelId()
    local size = dataTable.getDataTableByStringId("t_herospecial",modelId,"f_HeroSize")
    size = size/10000
    Util.Lua_SetLocalScaleXYZ(self.drawObj,size,size,1)
    local skeleton = self.drawObj:GetComponent("SkeletonGraphic")
    local bossAnimState = skeleton.AnimationState
    local anim = bossAnimState.Data.SkeletonData:FindAnimation("idle1")
    if anim then
        bossAnimState:SetAnimation(0,"idle1", false)
    end
    TBS.CameraMgr.EnableUltimateCamera(true)
    self:PlayAnim(self.animName)
    self:StartEffect()
end
function UltimateEffect:PlayAnim(animName)
    if  #self.animList > 0 and not string.IsNilOrEmpty(animName) then
        self.animName = animName
        local time = 0
        for k,v in pairs(self.animList) do
            local t = Util.PlayAction(v,animName,false,-1,true)
            if t > time then
                time = t
            end
        end
        if self.animEventCallback then
            for k,v in pairs(self.animList) do
                v:RegisterStateLuaUserFunction(self.animEventCallback)
            end
        end
        if self.isAutoDestroy then
            self.during = time
        end
    end
end
function UltimateEffect:Update(dt)
    TBS.EffectBase.Update(self,dt)
    if self.effectState == 1 then   --上升
        self.effectTime = self.effectTime + dt
        local t = self.effectTime / self.effectMoveTime
        if t >= 1 then
            t = 1
            self.effectState = 2
            self.effectTime = 0
        end
        t = 1-math.pow(math.abs(t-1),self.effectCurve)
        Vector3.Lerp2(self.effectMoveStartPos,self.effectMoveEndPos,t,self.effectPos)
        Util.Lua_SetLocalPositionXYZ(self.drawObj,self.effectPos.x,self.effectPos.y,0)
    elseif self.effectState == 2 then --停留
        self.effectTime = self.effectTime + dt
        if self.effectTime >= self.effectStayTime then
            self.effectState = 3
            self.drawObj:SetActive(false)
        end
    end
end
function UltimateEffect:StartEffect()
    self.effectState = 1
    Util.Lua_SetLocalPositionXYZ(self.drawObj,self.effectMoveStartPos.x,self.effectMoveStartPos.y,0)
end